<?php
class ModelVoucherVoucher extends Model 
{
	public function getList($where)
	{
		$query = $this->db->query("Select * from `voucher` where 1=1 ".$where);
		return $query->rows;
	}
	
	public function search($where = "")
	{
		$query = $this->db->query("Select `voucher`.*,`creator`.fullname as creatorname, `partner`.fullname, `partner`.address, `partner`.phone from `voucher`,`user` as `creator`, `user` as `partner` where `voucher`.employeeid = `creator`.userid  and partner.userid = `voucher`.partnerid ".$where);
		return $query->rows;
	}
	
	public function getDeptFromPartner($partnerid)
	{
		$arr = array();
		$query = $this->db->query("Select distinct `voucher`.partnerid, sum(`voucher`.grandtotal) as 'total', sum(`voucher`.paymentamount) as 'paid'   from `voucher` where `voucher`.partnerid = '".$partnerid."' group by `voucher`.partnerid");
		$arr = $query->row;
		return (float)$arr['total']-(float)$arr['paid'];
	}
	
	public function getItem($voucherid)
	{
		$query = $this->db->query("Select * from `voucher` where voucherid = '".$voucherid."'");
		return $query->row;
	}
	
	public function getItemByOrderId($orderid)
	{
		$query = $this->db->query("Select * from `voucher` where orderid = '".$orderid."'");
		return $query->row;
	}
	
	public function nextVoucherNo($prefix)
	{
		return $this->db->getNextIdVarChar("voucher","voucherno",$prefix);
	}
	
	public function save($data)
	{
		$voucherid=$this->db->escape(@$data['voucherid']);
		$voucherno=$this->db->escape(@$data['voucherno']);
		$voucherdate=$this->db->escape(@$data['voucherdate']);
		$totalpaid=(float)(@$data['totalpaid']);
		$deptamount=(float)(@$data['deptamount']);
		$grandtotal=(float)(@$data['grandtotal']);
		
		$description=$this->db->escape(@$data['description']);
		$partnerid=$this->db->escape(@$data['partnerid']);
		$discount=(float)(@$data['discount']);
		$employeeid=$this->db->escape(@$data['employeeid']);
		$vouchergroup=$this->db->escape(@$data['vouchergroup']);
		$orderid=$this->db->escape(@$data['orderid']);
		$ordernumber=$this->db->escape(@$data['ordernumber']);
		$paymenttype=$this->db->escape(@$data['paymenttype']);
		$vouchertype=$this->db->escape(@$data['vouchertype']);
		
		$incomeamount = 0;
		$expenseamount = 0;
		if($vouchertype == "EXPENSE")
		{
			$paymentamount=(float)(@$data['totalpaid'])*(-1);
			$expenseamount=(float)(@$data['totalpaid']);
			
		}else
		{
			$incomeamount=(float)(@$data['totalpaid']);
		}
		
		$field=array(
						'voucherno',
						'voucherdate',
						'totalpaid',
						'deptamount',
						'grandtotal',
						'paymentamount',
						'description',
						'partnerid',
						'discount',
						'employeeid',
						'vouchergroup',
						'orderid',
						'ordernumber',
						'paymenttype',
						'vouchertype',
						'expenseamount',
						'incomeamount'
					);
		$value=array(
						$voucherno,
						$voucherdate,
						$totalpaid,
						$deptamount,
						$grandtotal,
						$paymentamount,
						$description,
						$partnerid,
						$discount,
						$employeeid,
						$vouchergroup,
						$orderid,
						$ordernumber,
						$paymenttype,
						$vouchertype,
						$expenseamount,
						$incomeamount
					);
		
		$voucher = $this->getItem($voucherid);
		if(count($voucher) > 0)
		{
			$where="voucherid = '".$voucherid."'";
			$this->db->updateData("voucher",$field,$value,$where);
			return $voucherid;
		}
		else
		{
			return $this->db->insertData("voucher",$field,$value);	
		}
	}
			
	public function delete($voucherid)
	{
		$orderid=$this->db->escape(@$voucherid);
		$where="voucherid = '".$voucherid."'";
		$this->db->deleteData('voucher',$where);
	}
	
	public function deletes($data)
	{
		foreach($data as $orderid)
		{
			$this->delete($orderid);
		}		
	}
	
}

?>